package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.common.util.JsonUtils;
import com.heima.model.article.msg.ArticleBehaviorMsg;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.heima.common.constants.message.HotArticleConstants.HOT_ARTICLE_SCORE_STREAM_TOPIC;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author heima
 * @since 2021-03-23
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements IApLikesBehaviorService {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void likeBehavior(LikesBehaviorDto dto) {
        Boolean isUser=false;
        //1、获取用户id，判断是用户操作还是设备操作
        Integer userId = UserThreadLocalUtils.getUserId();
        if(userId!=null && userId!=0){
            //用户登录，是用户的操作
            isUser=true;
        }
        //2、参数校验
        if(userId==null && dto.getEquipmentId()==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //3、查询点赞记录，判断当前用户或者设备是否已经对当前文章点赞
        QueryWrapper<ApLikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApLikesBehavior::getArticleId,dto.getArticleId());
        if(isUser){
            //用户登录
            queryWrapper.lambda().eq(ApLikesBehavior::getApUserId,userId);
        }else{
            //用户未登录，通过设备号查询
            queryWrapper.lambda().eq(ApLikesBehavior::getEquipmentId,dto.getEquipmentId());
        }
        //执行查询
        ApLikesBehavior likesBehavior = this.getOne(queryWrapper);
        if(likesBehavior==null){
            //没有点赞，需要记录点赞行为
            likesBehavior = new ApLikesBehavior();
            likesBehavior.setArticleId(dto.getArticleId());
            if(isUser){
                likesBehavior.setApUserId(userId);
            }else{
                likesBehavior.setEquipmentId(dto.getEquipmentId());
            }
            likesBehavior.setType(LikesBehaviorDto.Type.ARTICLE.getCode());
            likesBehavior.setOperation(dto.getOperation());
            likesBehavior.setCreatedTime(new Date());
            this.save(likesBehavior);
        }else{
            //点赞了，更新点赞状态
            likesBehavior.setOperation(dto.getOperation());
            likesBehavior.setUpdateTime(new Date());
            this.updateById(likesBehavior);
        }
        //点赞行为，生成消息，记录行为分值
        if(dto.getOperation()==1){
            ArticleBehaviorMsg articleBehaviorMsg = new ArticleBehaviorMsg(
                    dto.getArticleId(),1,ArticleBehaviorMsg.ArticleBehaviorType.LIKES
            );
            kafkaTemplate.send(HOT_ARTICLE_SCORE_STREAM_TOPIC, JsonUtils.toString(articleBehaviorMsg));
        }
    }

    @Override
    public ApLikesBehavior queryLikeBehavior(Integer userId, String equipmentId, Long articleId, Integer likeType) {
        Boolean isUser=false;
        if(userId!=null && userId!=0){
            isUser=true;
        }
        QueryWrapper<ApLikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApLikesBehavior::getArticleId,articleId).eq(ApLikesBehavior::getType,likeType);
        if(isUser){
            queryWrapper.lambda().eq(ApLikesBehavior::getApUserId,userId);
        }else{
            queryWrapper.lambda().eq(ApLikesBehavior::getEquipmentId,equipmentId);
        }
        ApLikesBehavior likesBehavior = this.getOne(queryWrapper);
        return likesBehavior;
    }
}
